//---------------------------------------------------------------------------------------------------------------------------------------------------
// <copyright file="KeyboardControl.cs" company="DarkWynter Studios">
//     Copyright (C)2007 DarkWynter Studios.  All rights reserved.
// </copyright>
//---------------------------------------------------------------------------------------------------------------------------------------------------
// {Contact : darkwynter.com for licensing information
//---------------------------------------------------------------------------------------------------------------------------------------------------

namespace DarkWynter.Engine.Controllers
{
    #region Using Statements
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Input;
    using System.Diagnostics;
    //using DarkWynter.Engine.ObjectLib;
    //using DarkWynter.Engine.Menus;
    using Controllers;
    #endregion

    /// <summary>
    /// Keyboard event class for mapping a keyboard button to an event
    /// </summary>
    public class KeyboardControl
    {
        // Time delay between key presses to avoid double input
        private int KEY_TIMER_MAX_MILLI = 10;

        private Stopwatch controlTimer = new Stopwatch();

        /// <summary>
        /// Keyboard key we are checking for
        /// </summary>
        public Keys key = Keys.None;
        
        /// <summary>
        /// On click delegate type
        /// </summary>
        /// <param name="args">Controller boolean event arguments</param>
        public delegate void OnClickHandler(ControllerBoolEventArgs args);
        private event OnClickHandler click;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="keyboardkey">Keyboard key to watch</param>
        /// <param name="onClickHandler">Delegate to be invoked</param>
        /// <param name="timerValue">Time delay between multiple invocations</param>
        public KeyboardControl(Keys keyboardkey, OnClickHandler onClickHandler, int timerValue)
        {
            key = keyboardkey;                                  // Assign Key
            click += new OnClickHandler(onClickHandler);        // Assign delegate
            controlTimer.Start();                               // Start timer
            KEY_TIMER_MAX_MILLI = timerValue;                   // Assign time delay
        }

        /// <summary>
        /// Update Method
        /// </summary>
        /// <param name="keyboardState">Updated keyboard state</param>
        /// <param name="objectLibrary">Object Library</param>
        /// <param name="menuSystem">Menu System</param>
        public void Update(KeyboardState keyboardState, ref List<object> argObjects)
        {
            // Check double-input timer
            if (controlTimer.ElapsedMilliseconds > KEY_TIMER_MAX_MILLI)
            {
                // Reset timer
                controlTimer.Reset();
                controlTimer.Start();

                // If button was pressed
                if (keyboardState.IsKeyDown(key))
                {
                    if (click != null)
                    {
                        click(new ControllerBoolEventArgs(true, ref argObjects));
                    }
                }
            }
        }
    }
}

